//
//  AutoShowView.m
//  AutoShowView
//
//  Created by 冷悦 on 16/1/31.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "AutoShowView.h"
#import "UIView+Extension.h"
#import "UIImageView+FadeWebCache.h"
#ifndef kImagePlaceHolder
#define kImagePlaceHolder @"imagePlaceholder"
#endif

typedef  NS_ENUM(NSInteger, UIViewShowDirection) {
    UIViewShowDirectionLeft,
    UIViewShowDirectionRight,
    UIViewShowDirectionTop,
    UIViewShowDirectionBottom,
};

@interface AutoShowView ()

//需要展示的imageView
@property (nonatomic, strong) UIImageView *showImageView;

//动画使用，临时的imageView
@property (nonatomic, strong) UIImageView *transientImageView;

//除去当前显示的imageView 内容之外，其余的imageView
@property (nonatomic, strong) NSMutableArray *pickImages;

//当前需要显示的imageView 名字
@property (nonatomic, copy) NSString *imageName;

@end

@implementation AutoShowView

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame
                       images:(NSArray *)images
              andTimeInterval:(NSTimeInterval)timeInterval{
    [self setFrame:frame];
    self.images = images;
    self.timeInterval = timeInterval;
    
    return self;
}

#pragma mark - Public
- (void)updateImageView{
    [self updatePickImagesAndImageName];
    NSInteger imagesCount = self.pickImages == nil ? 0 : self.pickImages.count;
    
    if (self.timeInterval > 0 && imagesCount > 0) {
        int dir = arc4random() % 4;
        self.transientImageView.frame = self.frame;
        CGRect showImageViewFrame = self.showImageView.frame;
        
        if (dir == UIViewShowDirectionLeft) {
            self.transientImageView.x = -self.frame.size.width;
            showImageViewFrame.origin.x = self.frame.size.width;
        } else if (dir == UIViewShowDirectionRight) {
            self.transientImageView.x = self.frame.size.width;
            showImageViewFrame.origin.x = -self.frame.size.width;
        } else if (dir == UIViewShowDirectionTop) {
            self.transientImageView.y= -self.frame.size.height;
            showImageViewFrame.origin.y = self.frame.size.height;
        } else {
            self.transientImageView.y = self.frame.size.height;
            showImageViewFrame.origin.y = -self.frame.size.height;
        }
        
        [self.transientImageView setImageWithURLString:self.imageName andPlaceHolderString:kImagePlaceHolder];
        
        [self addSubview:self.transientImageView];
        
        [UIView animateWithDuration:self.timeInterval
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.transientImageView.frame = self.frame;
                             self.showImageView.frame = showImageViewFrame;
                         } completion:^(BOOL finished) {
                             UIImage *image = self.transientImageView.image;
                             self.showImageView.frame = self.frame;
                             self.showImageView.image = image;
                             [self.transientImageView removeFromSuperview];
                         }];
    }
}

#pragma mark - Private
/**
 *  更新pickImages 以及要展示的image
 */
- (void)updatePickImagesAndImageName{
    NSInteger imagesCount = self.images == nil ? 0 : self.images.count - 1;
    
    if (imagesCount > 0) {
        int imagePos = arc4random() % imagesCount;
        self.pickImages = [NSMutableArray arrayWithArray:self.images];
        [self.pickImages removeObject:self.imageName];
        self.imageName = [self.pickImages objectAtIndex:imagePos];
    }
}

#pragma mark - Setters
- (void)setImages:(nonnull NSArray *)images{
    _images = images;
    
    if (_images.count > 0) {
        self.imageName = [_images firstObject];
        [self updatePickImagesAndImageName];
        [self.showImageView setImageWithURLString:self.imageName andPlaceHolderString:kImagePlaceHolder];
    }
    
    self.showImageView.frame = self.frame;
}

#pragma mark - Getters
- (UIImageView *)showImageView{
    if (_showImageView == nil) {
        _showImageView = [[UIImageView alloc]init];
        _showImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_showImageView];
    }
    
    return _showImageView;
}

- (UIImageView *)transientImageView{
    if (_transientImageView == nil) {
        _transientImageView = [[UIImageView alloc]init];
        _transientImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return _transientImageView;
}

#pragma mark - overrride
- (void)drawRect:(CGRect)rect{
    self.showImageView.frame = rect;
}

- (instancetype)init{
    self = [super init];
    self.clipsToBounds = YES; //当值为YES时，子视图如果超过了当前视图的区域，超出的部分就会被裁掉。默认值是NO，也就是说当子视图显示区域大于主视图的时候还是正常显示不会裁剪。
    return self;
}

@end
